package com.hnzq.cloud.freshaddressassembly.util;

import com.hnzq.cloud.freshaddressassembly.domain.FreSite;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class WoniuBeanUtils {
    public static  <T> T copyObject(Object source,Class<T> type){
        T obj = null;
        try {
            obj = type.newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        BeanUtils.copyProperties(source,obj);
        return obj;
    }

    public static <T> List<T> copyList(Collection source, Class<T> type){
        ArrayList<T> list = new ArrayList<>();
        for (Object obj : source) {
            try {
                T t = type.newInstance();
                BeanUtils.copyProperties(obj,t);
                list.add(t);
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        return list;
    }
    public static List<FreSite> objToList(Object obj){
        List<FreSite> result = new ArrayList<FreSite>();
        if (obj instanceof ArrayList<?>) {
            for (Object o : (List<?>) obj) {
                result.add(FreSite.class.cast(o));
            }
        }
        return result;
    }
    private static final double EARTH_RADIUS = 6371.393;
    public static Double getNewDistance(double longitude1, double latitude1, double longitude2, double latitude2) {
        // 纬度
        double lat1 = Math.toRadians(latitude1);
        double lat2 = Math.toRadians(latitude2);
        // 经度
        double lng1 = Math.toRadians(longitude1);
        double lng2 = Math.toRadians(longitude2);
        // 纬度之差
        double a = lat1 - lat2;
        // 经度之差
        double b = lng1 - lng2;
        // 计算两点距离的公式
        double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) +
                Math.cos(lat1) * Math.cos(lat2) * Math.pow(Math.sin(b / 2), 2)));
        // 弧长乘地球半径, 返回单位: 千米
        s =  s * EARTH_RADIUS;
        return s * 1000;
    }

}
